/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.Checkin;
import model.entities.Customer;
import model.entities.Discount;
import model.entities.RoleSV;
import model.entities.Room;
import model.entities.RoomType;

/**
 *
 * @author Hieu.Nguyen
 */
public class CustomerDAO {
    private static CustomerDAO customerDAO;

    public static CustomerDAO getInstance() {
        if (customerDAO == null) {
            customerDAO = new CustomerDAO();
        }
        return customerDAO;
    }
    
     public List<Customer> getCustomer(boolean isActived) {
        List<Customer> listCustomer = new ArrayList<Customer>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblCustomer where isActived = 'true'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Customer r = new Customer();
                r.setCustomerID(rs.getInt("CustomerID"));
                
                r.setFirstName(rs.getString("FirstName"));
                
                r.setMiddleName(rs.getString("MiddleName"));
                
                r.setLastName(rs.getString("LastName"));
                
                r.setAddStreet(rs.getString("AddStreet"));
                
                r.setAddCity(rs.getString("AddCity"));
                
                r.setAddState(rs.getString("AddState"));
                
                r.setAddZip(rs.getInt("AddZip"));
                
                r.setAddCountry(rs.getString("AddCountry"));
                
                r.setHomePhone(rs.getInt("HomePhone"));
                
                r.setWorkPhone(rs.getInt("WorkPhone"));
                
                r.setEmail(rs.getString("Email"));

                r.setLastVisited(rs.getInt("LastVisited"));
                
                r.setcCNumber(rs.getInt("CCNumber"));
                
                r.setcCExpiry(rs.getDate("CCExpiry"));
                
                listCustomer.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listCustomer;
    }
     
     public Customer getCustomerByID(int customerID) {

        Customer cm = new Customer();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblCustomer where CustomerID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, customerID);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                
                cm.setCustomerID(rs.getInt("CustomerID"));
                cm.setFirstName(rs.getString("FirstName"));
                cm.setMiddleName(rs.getString("MiddleName"));
                cm.setLastName(rs.getString("LastName"));
                
                cm.setAddStreet(rs.getString("AddStreet"));
                cm.setAddCity(rs.getString("AddCity"));
                cm.setAddState(rs.getString("AddState"));
                cm.setAddZip(rs.getInt("AddZip"));
                cm.setAddCountry(rs.getString("AddCountry"));
                cm.setHomePhone(rs.getInt("HomePhone"));
                cm.setWorkPhone(rs.getInt("WorkPhone"));
                cm.setEmail(rs.getString("Email"));
                cm.setLastVisited(rs.getInt("LastVisited"));
                cm.setcCNumber(rs.getInt("CCNumber"));
                cm.setcCExpiry(rs.getDate("CCExpiry"));
    
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return cm;
     }
     public boolean createCustomer(Customer customer) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblCustomer values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            pstmt.setString(1, customer.getFirstName());
            pstmt.setString(2, customer.getMiddleName());
            pstmt.setString(3, customer.getLastName());
            pstmt.setString(4, customer.getAddStreet());
            pstmt.setString(5, customer.getAddCity());
            pstmt.setString(6, customer.getAddState());
            pstmt.setInt(7, customer.getAddZip());
            pstmt.setString(8, customer.getAddCountry());
            pstmt.setInt(9, customer.getHomePhone());
            pstmt.setInt(10, customer.getWorkPhone());
            pstmt.setString(11, customer.getEmail());
            pstmt.setInt(12, customer.getLastVisited());
            pstmt.setInt(13, customer.getcCNumber());
            pstmt.setDate(14, customer.getcCExpiry());
            pstmt.setBoolean(15, customer.isIsActived());
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
     }
     public boolean deleteCustomer(int customerID) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblCustomer set isActived = 'false' where CustomerID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, customerID);

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCheck;
    }
     public boolean updateCustomer(Customer customer) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblCustomer set FirstName = ?, MiddleName = ?, LastName = ?, AddStreet = ?, AddCity = ?, AddState = ?, AddZip = ?, AddCountry = ?, HomePhone = ?, WorkPhone = ?, Email = ?, LastVisited = ?, CCNumber = ? ,CCExpiry = ? where CustomerID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, customer.getFirstName());
            pstmt.setString(2, customer.getMiddleName());
            pstmt.setString(3, customer.getLastName());
            pstmt.setString(4, customer.getAddStreet());
            pstmt.setString(5, customer.getAddCity());
            pstmt.setString(6, customer.getAddState());
            pstmt.setInt(7, customer.getAddZip());
            pstmt.setString(8, customer.getAddCountry());
            pstmt.setInt(9, customer.getHomePhone());
            pstmt.setInt(10, customer.getWorkPhone());
            pstmt.setString(11, customer.getEmail());
            pstmt.setInt(12, customer.getLastVisited());
            pstmt.setInt(13, customer.getcCNumber());
            pstmt.setDate(14, customer.getcCExpiry());
            pstmt.setInt(15, customer.getCustomerID());

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
    }
     
    
    
}
